<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>会员订单</title>
</head>
<body>
<a href="index.html">返回主页</a>
<table border="1" cellpadding="0" cellspacing="0" style="text-align: center" width="1500px" id="tableDiv">
    <thead>
        <tr style="height: 80px">
            <th>订单号</th>
            <th>收货人</th>
            <th>收货地址</th>
            <th>收货人电话</th>
            <th>创建时间</th>
            <th>商品总金额</th>
            <th>支付方式</th>
            <th>支付状态</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>

    </tbody>
</table>
<script src="../js/jquery-3.3.1.js"></script>
<script src="../js/common.js"></script>
<script>

    var v_token;
    $(function (){
        v_token = sessionStorage.getItem(TOKEN);
        initOrder();
    })

    function initOrder(){
        if (v_token){
            $.ajax({
                type:"get",
                url:API_URL+"orders",
                beforeSend:function (xhr){
                    xhr.setRequestHeader(X_AUTH,v_token);
                },
                success:function (res){
                    if (res.code == 200){
                        var orderList = res.data;
                        var v_html = "";
                        for (let order of orderList) {
                            var payType = order.payType == 1?"支付宝":"微信"
                            var orderStatus = order.orderStatus == 0?"未付款":order.orderStatus == 10?"已支付,代发货":"交易关闭"
                            var operate = "<a href='#' onclick='cancelOrder(\""+order.id+"\")'>取消订单</a>/<a href='"+API_URL+"pay?orderId="+order.id+"'>支付</a>";
                            v_html += '<tr style="height: 80px">\n' +
                                '            <td>'+order.id+'</td>\n' +
                                '            <td>'+order.recipinentName+'</td>\n' +
                                '            <td>'+order.address+'</td>\n' +
                                '            <td>'+order.phone+'</td>\n' +
                                '            <td>'+order.createTime+'</td>\n' +
                                '            <td>'+order.totalPrice+'</td>\n' +
                                '            <td>'+payType+'</td>\n' +
                                '            <td>'+orderStatus+'</td>\n' +
                                '            <td>'+operate+'</td>\n' +
                                '        </tr>';
                        }
                        $("#tableDiv tbody").html(v_html);
                    }
                }
            })
        }
    }

    function cancelOrder(id){
        if (v_token){
            $.ajax({
                type:"post",
                url:API_URL+"orders/cancel/"+id,
                beforeSend:function (xhr){
                    xhr.setRequestHeader(X_AUTH,v_token);
                },
                success:function (res){
                    if (res.code == 200){
                        initOrder();
                    }
                }
            })
        }
    }

</script>


</body>
</html>